﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;

public partial class Common_News_NewsDetail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
            BindDataDetail();
            hplBack.NavigateUrl = "~/Common/News/ViewAllNews.aspx?KindNo=" + Request.QueryString["KindNo"];
        }

    }

 

   //public string CleanHtml(object Html) 
   //{
   //     var s = Html.ToString();
   //     var b = new StringBuilder();
   //     foreach (char c in s) {
   //         var l = b.Length;
    		
   //         // Known Replacements
   //         if (c == '&') {
   //             b.Append("&amp;");
   //         }
   //         if (c == '<') {
   //             b.Append("&lt;");
   //         }
   //         if (c == '>') {
   //             b.Append("&gt;");
   //         }
   //         if (c == '`') {
   //             b.Append("'");
   //         }
    		
   //         // Always Encode Extended Ascii Codes
   //         if ((int)(c) > 127) {
   //             b.Append("&#" + (int)(c) + ";");
   //         }
    		
   //         // If nothing was added, add the original char
   //         if (l == b.Length) {
   //             b.Append(c);
   //         }
   //     }
   //     return b.ToString();
   // }


    private string LimitWord(string value)
    {
        int length = 700;
        if (value.Length > length)
            return value.Substring(0, length) + "...";
        else
            return value;

    }
    private void BindData()
    {
        PccMsg msg = new PccMsg();
        string NewsID = "0";
        if (Request.QueryString["NewsID"] != "" && Request.QueryString["NewsID"] != null)
            NewsID = Request.QueryString["NewsID"];

        msg.CreateFirstNode("NewsID", NewsID);
        msg.CreateFirstNode("Title", "");
        msg.CreateFirstNode("KindNo", Request.QueryString["KindNo"]);
        msg.CreateFirstNode("Status", "N");
        msg.CreateFirstNode("ViewCount", "1");
        clsWebShop cls = new clsWebShop();
        DataSet ds = null;
        ds = cls.Get_NewsByCondition(msg.GetXmlStr);
        dtlNews.DataSource = ds;
        dtlNews.DataBind();

        

    }
    private void BindDataDetail()
    {
        PccMsg msg = new PccMsg();
        string NewsID = "0";
        if (Request.QueryString["NewsID"] != "" && Request.QueryString["NewsID"] != null)
            NewsID = Request.QueryString["NewsID"];

        msg.CreateFirstNode("NewsID", NewsID);
        msg.CreateFirstNode("Title", "");
        msg.CreateFirstNode("KindNo", Request.QueryString["KindNo"]);

        clsWebShop cls = new clsWebShop();
        DataSet ds = null;
        ds = cls.Get_SimilarNewsByCondition(msg.GetXmlStr);
        grv_SimilarNews.DataSource = ds;
        grv_SimilarNews.DataBind();



    }
    protected void dtlNews_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            Label lblDate = (Label)e.Item.FindControl("lblDate");
            lblDate.Text = "Cập nhật ngày: " + FormatDate(lblDate.Text);


           /* Image imgNews = (Image)e.Item.FindControl("imgNews");
            string ImgUrl = DataBinder.Eval(e.Item.DataItem, "ImgUrl").ToString();
            if (ImgUrl == "" || ImgUrl == null)
                ImgUrl = "~/Images/Body/Noimages.gif";
            else
                ImgUrl = "~/" + ImgUrl;
            imgNews.ImageUrl = ImgUrl;
            */
        }
    }
    private string FormatDate(string sDate)
    {
        string sReturn = "";
        try
        {
            sReturn = sDate.ToString().Substring(6, 2) + "/" + sDate.ToString().Substring(4, 2) + "/" + sDate.ToString().Substring(0, 4);
        }
        catch (Exception ex) { }

        return sReturn;
    }
    protected void grv_SimilarNews_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblDate = (Label)e.Row.FindControl("lblDate");
            lblDate.Text = "Cập nhật ngày: " + FormatDate(lblDate.Text);
        }
    }
    protected void grv_SimilarNews_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grv_SimilarNews.PageIndex = e.NewPageIndex;
        BindDataDetail();
    }
}
